package com.life.ai.server.common.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.aliyun.oss.ServiceException;

import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * FileUtils
 *
 * @author kylewka
 * @since 2023/12/13
 */
public class FileUtils extends FileUtil {

    public static void toZip(ZipOutputStream zipOutputStream, InputStream inputStream, String alias) {
        try {
            //设置压缩包内文件的名称
            zipOutputStream.putNextEntry(new ZipEntry(alias));
            IoUtil.copy(inputStream, zipOutputStream);
            zipOutputStream.closeEntry();
        } catch (Exception e) {
            throw new ServiceException("文件压缩失败");
        } finally {
            //关闭资源
            IoUtil.close(inputStream);
        }
    }
}
